package com.weipeep.sys.controller;


import java.util.Arrays;
import java.util.List;

import com.weipeep.sys.domain.RoleDO;
import com.weipeep.sys.domain.UserDO;
import com.weipeep.sys.service.RoleService;
import com.weipeep.sys.service.UserService;
import com.weipeep.sys.vo.StaffVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.weipeep.sys.domain.StaffDO;
import com.weipeep.sys.service.StaffService;
import com.weipeep.common.utils.Result;

/**
 * 
 * <pre>
 * 
 * </pre>
 * <small> 2018-04-19 11:24:57 | wangyun</small>
 */
@Controller
@RequestMapping("/sys/staff")
public class StaffController {
	@Autowired
	private StaffService staffService;
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@GetMapping()
	@RequiresPermissions("sys:staff:staff")
	String Staff(){
	    return "sys/staff/staff";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("sys:staff:staff")
	public Result<Page<StaffVo>> list(Integer pageNumber, Integer pageSize, StaffVo staff){
		// 查询列表数据
        Page<StaffVo> page = new Page<>(pageNumber, pageSize);
        page = staffService.selectStaffPage(page, staff);
        int total = staffService.selectStaffCount(staff);
        page.setTotal(total);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("sys:staff:add")
	String add(Model model){
		Wrapper wrapper = new EntityWrapper();
		wrapper.eq("level",1);
		List<RoleDO> roles = roleService.selectList(wrapper);
		model.addAttribute("roles", roles);
	    return "sys/staff/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("sys:staff:edit")
	String edit(@PathVariable("id") Long id,Model model){
		StaffDO staff = staffService.selectById(id);
		UserDO user = userService.selectById(staff.getUserId());
		Wrapper wrapper = new EntityWrapper();
		wrapper.eq("level",1);
		List<RoleDO> roles = roleService.findListByUserId(staff.getUserId(),wrapper);
		model.addAttribute("roles", roles);
		model.addAttribute("user", user);
		model.addAttribute("staff", staff);
	    return "sys/staff/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("sys:staff:add")
	public Result<String> save( StaffVo staff){
		if(staffService.insertStaff(staff)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("sys:staff:edit")
	public Result<String>  update( StaffVo staff){
		staffService.updateStaff(staff);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("sys:staff:remove")
	public Result<String>  remove( Long id){
		if(staffService.deleteStaffById(id)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("sys:staff:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		staffService.deleteStaffBatchIds(Arrays.asList(ids));
		return Result.ok();
	}
	
}
